/*
 * @Author: PanZhangOne
 * @Date: 2017-12-17 13:43:36
 * @Last Modified by: PanZhangOne
 * @Last Modified time: 2017-12-17 16:26:45
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;

const TopicSchema = new Schema({
  title: {
    type: String,
    minlength: [5, '主题不能少于5个字']
  },
  authorId: { type: ObjectId, ref: 'User' },
  tag: { type: ObjectId, ref: 'topicTag' },
  content: {
    type: String,
    required: true
  },
  lastReplyAt: { type: Date },
  createAt: { type: Date, default: Date.now() },
  updateAt: { type: Date },
  good: { type: Boolean },
  top: { type: Boolean },
  lock: { type: Boolean },
  replyCount: { type: Number, default: 0 },
  visitCount: { type: Number, default: 0 }
});

TopicSchema.pre('save', (next) => {
  let topic = this;
  topic.updateAt = Date.now();
  next();
});

module.exports = mongoose.model('topic', TopicSchema);
